package cn.melon.view.control;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import cn.melon.mod.sc.ScRequest;
import cn.melon.mod.store.StoreService;
import cn.melon.mod.store.StoreServiceImpl;
import cn.melon.window.MainWindow;

/**
 * @ClassName: ChangeToSubSubWindow
 * @author Hzy
 * 各内容页里的左边栏按钮的监听器，用于在选择时改变背景颜色、切换页面
 */
public class ChangeToSubSubWindow implements MouseListener {
    CardLayout card;
    JPanel parent;
    String subWindow;
    MainWindow m;
    
    public ChangeToSubSubWindow(MainWindow m, CardLayout card, JPanel parent, String subWindow) {
        this.m = m;
        this.card = card;
        this.parent = parent;
        this.subWindow = subWindow;
    }

    public ChangeToSubSubWindow(CardLayout card, JPanel parent, String subWindow) {
        this.card = card;
        this.parent = parent;
        this.subWindow = subWindow;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        JToggleButton btn = (JToggleButton) e.getComponent();
        MainWindow w = (MainWindow) btn.getRootPane().getParent();
        btn.setBackground(new Color(232, 232, 236));
        btn.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(200, 200, 200)));
        StoreService storeService = new StoreServiceImpl();
        if (card != null) {
            // 存在card为空的情况，比如图书馆只设置了一个页面，无法切换，则card传入时为null
            if (subWindow.equals("everydayUsePanel")) {
                // 日用百货更新请求
                System.out.println("1");
                storeService.getGoodsTable(w, "日用百货", 0, 30, "", "");
            }
            if (subWindow.equals("stationeryPanel")) {
                // 学习用具更新请求
                System.out.println("1");
                storeService.getGoodsTable(w, "学习用具", 0, 30, "", "");
            }
            if (subWindow.equals("snacksPanel")) {
                // 零食饮料更新请求
                System.out.println("1");
                storeService.getGoodsTable(w, "零食饮料", 0, 30, "", "");
            }
            if (subWindow.equals("referenceBooksPanel")) {
                // 学习书籍更新请求
                System.out.println("1");
                storeService.getGoodsTable(w, "学习资料", 0, 30, "", "");
            }
            if (subWindow.equals("lessonTablePanel")) {
                // 更新课表界面
                ScRequest request = new ScRequest(m);
                m.removeAllLessonInTable();
                request.getAllInfoResult();
            }
            if (subWindow.equals("chooseLessonPanel")) {
                ScRequest request = new ScRequest(m);
                request.getGetAllScInfoResult();
                m.removeAllLessonsInChooseWindow();
                // 更新选课界面
            }
            card.show(parent, subWindow);
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // 设置被选中时的背景颜色
        JToggleButton btn = (JToggleButton) e.getComponent();
        UIManager.put("ToggleButton.select", new Color(240, 240, 240));
        SwingUtilities.updateComponentTreeUI(btn);
        // 与内容页同色
        btn.setBackground(new Color(232, 232, 236));
        btn.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(200, 200, 200)));
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // 恢复原来的颜色
        JToggleButton btn = (JToggleButton) e.getComponent();
        btn.setBackground(new Color(226, 220, 246));
        btn.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(200, 200, 200)));

    }

    public void mousePressed(MouseEvent e) {

    }

    public void mouseReleased(MouseEvent e) {

    }
}
